package biz.datalk.industrialland.admin.config;

import biz.datalk.industrialland.admin.interceptor.CustomCheckSignatureInterceptor;
import biz.datalk.industrialland.common.advice.AppSecurityInfo;
import biz.datalk.industrialland.common.advice.cfg.DatalkCryptoCfgs;
import biz.datalk.industrialland.common.advice.mapper.IAppEncryptSecurityKeyMapper;
import biz.datalk.industrialland.common.component.AppEncryptSecurityKeyComponent;
import biz.datalk.industrialland.common.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tarofang@163.com
 * @date 2022-10-10
 */
@Slf4j
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class CustomRequestDecryptResponseEncryptConfig {


    @Bean
    public CustomCheckSignatureInterceptor checkSignatureInterceptor(RedisUtil redisUtil, DatalkCryptoCfgs datalkCryptoCfgs) {
        log.debug("checkSignatureInterceptor ====================================== into");
        try {
            return new CustomCheckSignatureInterceptor(redisUtil, datalkCryptoCfgs);
        } finally {
            log.debug("checkSignatureInterceptor ====================================== out");
        }
    }

    @Bean
    public AppEncryptSecurityKeyComponent appEncryptSecurityKeyComponent( //
            RedisUtil redisUtil, //
            IAppEncryptSecurityKeyMapper appEncryptSecurityKeyMapper, //
            DatalkCryptoCfgs datalkCryptoCfgs) {
        log.debug("appEncryptSecurityKeyComponent ====================================== into");
        try {

            return new AppEncryptSecurityKeyComponent(redisUtil, appEncryptSecurityKeyMapper, datalkCryptoCfgs) {
                private AppSecurityInfo appSecurityInfo = null;

                @Override
                public void initCache() {
                    // do nothing
                }

                @Override
                public AppSecurityInfo getByAppKey(String appKey) {
                    if (this.appSecurityInfo == null) {
                        AppSecurityInfo appSecurityInfo = new AppSecurityInfo();
                        appSecurityInfo.setId(0L);
                        appSecurityInfo.setAppKey(appKey);
                        appSecurityInfo.setClientPublicKey(datalkCryptoCfgs.getClient().getPublicKey());
                        appSecurityInfo.setClientPrivateKey(datalkCryptoCfgs.getClient().getPrivateKey());
                        appSecurityInfo.setServerPublicKey(datalkCryptoCfgs.getServer().getPublicKey());
                        appSecurityInfo.setServerPrivateKey(datalkCryptoCfgs.getServer().getPrivateKey());
                        this.appSecurityInfo = appSecurityInfo;
                    }

                    return this.appSecurityInfo;
                }
            };

        } finally {
            log.debug("appEncryptSecurityKeyComponent ====================================== out");
        }
    }

}
